package com.hnxxxy.sysmgr.mapper;

import java.util.List;

import com.hnxxxy.common.redis.RedisMyabtisCstCache;
import com.hnxxxy.entity.TokenExample;
import com.hnxxxy.sysmgr.pojo.Token;
import com.hnxxxy.sysmgr.vo.TokenVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@CacheNamespace(implementation = RedisMyabtisCstCache.class)
public interface TokenMapper {
    long countByExample(TokenExample example);

    int deleteByExample(TokenExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Token record);

    int insertSelective(Token record);

    List<Token> selectByExample(TokenExample example);

    Token selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Token record, @Param("example") TokenExample example);

    int updateByExample(@Param("record") Token record, @Param("example") TokenExample example);

    int updateByPrimaryKeySelective(Token record);

    int updateByPrimaryKey(Token record);



    /**
     * 查询权限
     * @param token
     * @return
     */
    @Select({"<script>",
            "select * from sys_token ",
            "<where> ",
            "<if test='tokenname!=null'>",
            "and tokenname like concat('%',#{tokenname},'%')",
            "</if>",
            "<if test='tokenvalue!=null'>",
            "and tokenvalue like concat('%',#{tokenvalue},'%')",
            "</if>",
            "</where>",
            "</script>"})
    List<Token> queryAllToken(Token token);

    /**
     * 添加权限
     * @param tokenVo
     */
    @Insert({"insert into sys_token (tokenname, type, tokenvalue, createdev, createdate,state,menu_id)" +
            " values (#{tokenname},#{type},#{tokenvalue},#{createdev},#{createdate},#{state},#{menu_id})"})
    void insertToken(TokenVo tokenVo);

    /**
     * 删除权限
     * @param id
     */
    @Delete({"delete from sys_token where id = #{id}"})
    void deleteToken(Integer id);

    /**
         * 更新权限信息
     * @param tokenVo
     * @return
     */
    @Update({"<script>",
            "update sys_token",
            "<set>",
            "<if test='type!=null'>",
            " type = #{type},",
            "</if>",
            "<if test='tokenname!=null'>",
            "tokenname=#{tokenname},",
            "</if>",
            "<if test='tokenvalue!=null'>",
            " tokenvalue=#{tokenvalue},",
            "</if>",
            "<if test='createdev!=null'>",
            "createdev=#{createdev},",
            "</if>",
            "<if test='state!=null'>",
            "state=#{state},",
            "</if>",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int updateToken(TokenVo tokenVo);

    /**
     * 根据pid查询权限数量
     * @param pid
     * @return
     */
    @Select({"select count(1) from sys_token where parentid=#{pid}"})
    Integer queryTokenByPid(Integer pid);

    /**
     *  根据角色ID查询菜单
     * @param roleid
     * @return
     */
    @Select({"select t1.* from sys_token t1 inner join sys_role_token t2 on(t1.id=t2.tokenid) where t2.roleid=#{roleid}"})
    List<Token> queryTokenByRoleId(Integer roleid);
}